Use XQuery with Flare Files

XQuery is another XML query language. XQuery uses XPath expressions, so XPath 2.0 is included with XQuery 1.0. The World Wide Web Consortium (W3C) published XQuery 1.0: An XML Query Language as recommendation in 2007 and edited to XQuery 1.0: An XML Query Language (Second Edition) in 2010. The W3C recommendation is posted at:

http://www.w3.org/TR/xquery/

To use XQuery, you need an XQuery implementation. W3C lists implementations known to W3C at:

http://www.w3.org/XML/Query/#implementations

To demonstrate XQuery, we will use a freely available technology. As of writing this, Microsoft SQL Server 2012 Express has an implementation of XQuery. You can download the database, tools, or both. But don’t feel limited to using the SQL Server implementation to learn about XQuery.

In the XPath post, we queried a Flare TOC for the Link attribute nodes for every TocEntry element. But we retrieved the attribute node. We could not filter for just the attribute value. With XQuery, we can accomplish that. Here is an XQuery code executed with Transact-SQL to create hyperlinks for every TOC entry in a generated Flare TOC in the Data folder of an HTML5 output.

DECLARE @FLTOC XML

SET @FLTOC =
'<?xml version="1.0" encoding="utf-8"?>
<CatapultToc
  Version="1">
  <TocEntry
    Title="Test Topic 1"
    Link="/Content/TestTopic1.htm" />
  <TocEntry
    Title="Test Topic 2"
    Link="/Content/TestTopic2.htm" />
  <TocEntry
    Title="Test Topic 3"
    Link="/Content/TestTopic3.htm" />
</CatapultToc>'

SELECT @FLTOC.query('for $TocEntry in (//TocEntry) 
			return <a href="{$TocEntry/@Link}">{string($TocEntry/@Title)}</a>') AS Links

The output is:

<a href="/Content/TestTopic1.htm">Test Topic 1</a>
<a href="/Content/TestTopic2.htm">Test Topic 2</a>
<a href="/Content/TestTopic3.htm">Test Topic 3</a>

But there is a pitfall here. Not every TocEntry element has a link attribute. So be careful. If you are working with a FLTOC file in a project, there is another pitfall. Not every link is to a topic file. So be even more careful there.

Leave a comment

Your email address will not be published.

HTML tags are not allowed.

253,324 Spambots Blocked by Simple Comments